﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using wms.Common;

namespace wms.Pages
{
    /// <summary>
    /// GoodsInput.xaml 的交互逻辑
    /// </summary>
    public partial class ManagerInput : Window
    {
        private bool modify = true;
        private Dictionary<string, object> _data;
        private int type = 1;

        public ManagerInput(Dictionary<string, object> data = null)
        {
            if (data == null)
            {
                modify = false;
                data = new Dictionary<string, object>();
                data.Add("name", "");
                data.Add("password", "");
                data.Add("phone", "");
                data.Add("sex", "男");
                data.Add("type", 1);
            }
            this.Data = data;
            ChangeLanguage.changePage(Variable.LanguageSeesion.ToString() );
            InitializeComponent();
            this.DataContext = this.Data;
            this.cbx_sex.SelectedIndex = data["sex"].ToString() == "男" ? 0 : 1;
            this.txt_password.Password = data["password"].ToString();
            this.type = (int)Utils.ParseInt(data["type"]);
        }

        private void btn_save_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string name = Utils.Parse(this.Data["name"]);
                string password = this.txt_password.Password;
                string sex = ((ComboBoxItem)this.cbx_sex.SelectedValue).Content as string;
                string phone = Utils.Parse(this.Data["phone"]);


                if (string.IsNullOrWhiteSpace(name))
                {
                    MessageBox.Show("名称不能为空");
                    return;
                }
                if (string.IsNullOrWhiteSpace(password))
                {
                    MessageBox.Show("密码不能为空");
                    return;
                }

                if (modify == false)
                {
                    string sql = "insert into t_manager(name,sex,password,phone,type) values(@name,@sex,@password,@phone,1)";
                    int? id = DBHelper.Insert(sql, new Dictionary<string, object> { { "@name", name }, { "@sex", sex }, { "@password", password }, { "@phone", phone } });

                    if (id != null)
                    {
                        MessageBox.Show("保存成功");
                        this.Close();
                    }
                }
                else
                {
                   //超级管理员
                    if (type == 0)
                    {
                        string sql = "update t_manager set password = @password,phone = @phone where id = @id and name = @name";
                        int count = DBHelper.ExcuteSQL(sql, new Dictionary<string, object> { { "@name", Auth.Name }, { "@password", password }, { "@phone", phone }, { "@id", this.Data["id"] } });
                        if (count == 1)
                        {
                            MessageBox.Show("保存成功");
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("您无权修改此用户信息");
                        }
                    }
                    else {
                        string sql = "update t_manager set name = @name,sex = @sex,password = @password,phone = @phone where id = @id and type != 0";
                        int count = DBHelper.ExcuteSQL(sql, new Dictionary<string, object> { { "@name", name }, { "@sex", sex }, { "@password", password }, { "@phone", phone }, { "@id", this.Data["id"] } });
                        if (count == 1)
                        {
                            MessageBox.Show("保存成功");
                            this.Close();
                        }
                    }
                   
                }
            }
            catch (Exception ex) {
                MessageBox.Show("保存失败\n" + ex.Message);
            }
        }

        private void btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        public Dictionary<string, object> Data
        {
            get
            {
                return _data;
            }

            set
            {
                _data = value;
            }
        }
    }
}
